package com.deke.course.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deke.course.dao.EduCourseCheckDao;
import com.deke.course.dao.EduCourseDao;
import com.deke.course.dao.EduCourseDescriptionDao;
import com.deke.course.dao.EduSubjectDao;
import com.deke.course.service.EduCheckService;
import com.deke.course.vo.CourseVo;
import com.deke.entity.EduCourse;
import com.deke.entity.EduCourseDescription;
import com.deke.entity.TbCheckCourse;
import com.deke.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @version 1.0
 * @Author :LiM
 * @Date :2022/12/11 21:08
 */
@Service
public class EduCheckServiceImpl implements EduCheckService {
    @Autowired
    private EduCourseDao courseDao;
    @Autowired
    private EduSubjectDao eduSubjectDao;
    @Autowired
    private EduCourseCheckDao courseCheckDao;
    @Autowired
    private EduCourseDescriptionDao descriptionDao;
    @Override
    public Result<IPage<EduCourse>> findByConditionPage(CourseVo courseVo, Integer current, Integer pageSize) {
        IPage<EduCourse> page = new Page(current,pageSize);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(courseVo.getTitle())){
            wrapper.like("title",courseVo.getTitle());
        }
        if (StringUtils.hasText(courseVo.getStatus())){
            wrapper.eq("status",courseVo.getStatus());
        }

        IPage<EduCourse> ipage =courseDao.selectPage(page, wrapper);
        for (EduCourse record : ipage.getRecords()) {
            String checkstatus = courseCheckDao.selectStatus(record.getId());
            record.setCheckStatus(checkstatus);
            String description = descriptionDao.selectdescriptionById(record.getId());
            record.setDescription(description);
            String subjectName = eduSubjectDao.selectSubjectById(record.getSubjectId());
            record.setSubjectName(subjectName);
            if(!record.getSubjectParentId().equals("0")){
                String subjectName2 = eduSubjectDao.selectSubjectById(record.getSubjectParentId());
                String quan=subjectName2+">"+subjectName;
                record.setSubjectName(quan);
            }
        }
        return new Result(200,"查询成功",ipage);
    }

    @Override
    public Result updateCourse(CourseVo courseVo) {
        EduCourse course=new EduCourse();
        course.setId(courseVo.getId());
        course.setTitle(courseVo.getTitle());
        course.setPrice(courseVo.getPrice());
        int i = courseDao.updateById(course);
        EduCourseDescription description=new EduCourseDescription();
        description.setId(courseVo.getId());
        description.setDescription(courseVo.getDescription());
        int a = descriptionDao.updateById(description);
        if(i>0&&a>0){
            return new Result(200,"修改成功");
        }
        return new Result(000,"修改失败");
    }

    /**
      * 审核状态的修改
     * @Return
     * @Author Lim
     * @Date 2022/12/20 13:59
     */
    @Override
    public Result updateCourseCheck(CourseVo courseVo) {
        int i = courseCheckDao.updateByCourseId(courseVo.getId(), courseVo.getCheckStatus(), courseVo.getCheckContent());
        if(i>0){
            return new Result(200,"修改成功");
        }
        return new Result(000,"修改失败");
    }

    @Override
    public Result changeStatus(Map map) {
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(map.get("id").toString());
        eduCourse.setStatus(map.get("status").toString());
        int update=courseDao.updateById(eduCourse);
        if(update>0){
            return new Result(200,"修改上下架成功");
        }
        return new Result(000,"修改状态失败");
    }
}
